<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px" size="small">
      <el-form-item label="采集项名称" prop="collectName">
        <el-input v-model="dataForm.collectName" placeholder="采集项名称"></el-input>
      </el-form-item>
    <el-form-item label="采集项值" prop="collectType">
      <el-input v-model="dataForm.collectType" placeholder="采集项值"></el-input>
    </el-form-item>
    <el-form-item label="采集项单位" prop="collectUnit">
      <el-input v-model="dataForm.collectUnit" placeholder="采集项单位"></el-input>
    </el-form-item>
    <el-form-item label="能耗类型" prop="meterType">
      <el-select v-model="dataForm.meterType"  style="width: 100px;" placeholder="请选择" clearable>
        <el-option
          v-for="(item, index) in meterTypeList" :key="index"
          :label="item.paramValue"
          :value="item.paramKey">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="采集项类型" prop="cumulativeType">
      <el-radio-group v-model="dataForm.cumulativeType">
        <el-radio label="1">累计</el-radio>
        <el-radio label="0">瞬时</el-radio>
      </el-radio-group>
    </el-form-item>
      <el-form-item label="是否展示" prop="collectStatus">
        <el-radio-group v-model="dataForm.collectStatus">
          <el-radio label="1">是</el-radio>
          <el-radio label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false" size="small">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" size="small">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        meterTypeList:[],
        visible: false,
        dataForm: {
          id: 0,
          collectType: '',
          collectName: '',
          collectUnit: '',
          meterType: '',
          cumulativeType: '1',
          remark: '',
          collectStatus: '1'
        },
        dataRule: {
          collectType: [
            { required: true, message: '采集项值不能为空', trigger: 'blur' }
          ],
          collectName: [
            { required: true, message: '采集项名称不能为空', trigger: 'blur' }
          ],
          collectUnit: [
            { required: true, message: '采集项单位不能为空', trigger: 'blur' }
          ],
          meterType: [
            { required: true, message: '能耗类型', trigger: 'blur' }
          ],
          cumulativeType: [
            { required: true, message: '采集项类型', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          collectStatus: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,meterTypeList) {
        this.meterTypeList =meterTypeList
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecscollectitem/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.collectType = data.ecscollectitem.collectType
                this.dataForm.collectName = data.ecscollectitem.collectName
                this.dataForm.collectUnit = data.ecscollectitem.collectUnit
                this.dataForm.meterType = data.ecscollectitem.meterType
                this.dataForm.cumulativeType = data.ecscollectitem.cumulativeType
                this.dataForm.remark = data.ecscollectitem.remark
                this.dataForm.collectStatus = data.ecscollectitem.collectStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.okhttp({
              url: this.$http.adornUrl(`/ecs/ecscollectitem/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'collectType': this.dataForm.collectType,
                'collectName': this.dataForm.collectName,
                'collectUnit': this.dataForm.collectUnit,
                'meterType': this.dataForm.meterType,
                'cumulativeType': this.dataForm.cumulativeType,
                'remark': this.dataForm.remark,
                'collectStatus': this.dataForm.collectStatus,
                "customerId": this.$util.cookies.get('customer_id'),
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
